<script setup lang="ts">
import { useRoute } from "vue-router";
import { ref, reactive, onMounted, nextTick } from "vue";
import { type FormInstance, ElMessage, ElMessageBox } from "element-plus";

import {
  tableButtons,
  operationButtons,
  columns1,
  columns2,
  columns3
} from "./constant";
import {
  getProjectBudgetList,
  deleteProjectBudget,
  getProjectNatureType,
  getWinBidListNV
} from "@/api/projectBudget";
import { exportExcel } from "@/api/exportAll";
import { TableProBar } from "@/components/ReTable";
import DialogForm from "./components/DialogForm.vue";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import DescriptionsDialog from "./components/DescriptionsDialog.vue";
import { TableColumOperation } from "@/components/TableColumOperation";
import { getSellContractListNVByProjectId } from "@/api/projectAccount";

defineOptions({
  name: "Budget"
});

const title = useRoute().meta.title as string;

// 查询条件模型 vue3 声明对象类型的响应式变量，另外如果是原始数据类型，则使用ref方法
const queryForm = reactive({
  pageIndex: 1,
  pageSize: 20,
  projectId: "",
  sellContractId: "",
  projectNature: ""
});
const totalPage = ref(0); // 数据总页数
const tableData = ref([]); // Table组件显示数据
const tableHeight = ref(0); // Table组件高度
const projectList = ref([]); // 项目列表
const queryFold = ref(true); // 查询条件折叠状态
const queryItemMaxNum = ref(1); // 查询项默认显示最大数量
const sellContractList = ref([]); // 项目列表
const dialogFormRef = ref(null); // 表单对话框组件实例
const requestLoading = ref(true); // 请求加载状态
const projectNatureList = ref([]); // 项目列表
const descriptionsDialogRef = ref(null); // 表单对话框组件实例
const queryFormRef = ref<FormInstance>(); // 查询条件表单组件实例
//序号列
function getIndex(index) {
  const page = queryForm.pageIndex;
  const pagesize = queryForm.pageSize;
  return (page - 1) * pagesize + index + 1;
}
// 获取表格数据
function onSearch() {
  requestLoading.value = true;
  getProjectList();
  getProjectBudgetList(queryForm)
    .then(({ data }) => {
      const _data = data.data || [];
      tableData.value = _data.map(item => {
        item.disabled = {};
        item.title = {};
        // //编辑
        // item.disabled["handleEdit"] = item.bidStatus == 3;
        // item.title["handleEdit"] =
        //   item.bidStatus == 3 ? "已中标项目不可编辑" : "";
        // //删除
        // item.disabled["handleDelete"] = item.bidStatus == 3;
        // item.title["handleDelete"] =
        //   item.bidStatus == 3 ? "已中标项目不可删除" : "";
        return item;
      });
      console.log("tableData", tableData.value);
      totalPage.value = data.totalCount;
      requestLoading.value = false;
    })
    .catch(() => {
      requestLoading.value = false;
      tableData.value = [];
    });
}

// 获取项目列表数据
function getProjectList() {
  getWinBidListNV()
    .then(({ data }) => {
      projectList.value = data || [];
    })
    .catch(() => {
      projectList.value = [];
    });
}

// 获取项目列表数据
function getProjectNatureList() {
  getProjectNatureType(queryForm)
    .then(({ data }) => {
      projectNatureList.value = data || [];
    })
    .catch(() => {
      projectNatureList.value = [];
    });
}

// 获取合同列表数据
function getSellContractListByProjectId(projectId) {
  getSellContractListNVByProjectId({ projectId })
    .then(({ data }) => {
      sellContractList.value = data || [];
    })
    .catch(() => {
      sellContractList.value = [];
    });
}

function handleProjectChange(val) {
  if (val) {
    getSellContractListByProjectId(val);
  } else {
    sellContractList.value = [];
  }
  queryForm.sellContractId = "";
}

//切换“折叠”与“展开”
function handleFold() {
  queryFold.value = !queryFold.value;
  setTableHeight();
}

// 重置查询条件表单
const handleReset = () => {
  sellContractList.value = [];
  queryFormRef.value.resetFields();
  onSearch();
};

function handleOperation(functionName, row?) {
  console.log("functionName", functionName);
  switch (functionName) {
    case "handleAdd":
      handleAdd();
      break;
    case "handleExport":
      handleExport();
      break;
    case "handleExportOne":
      handleExportOne(row);
      break;
    case "handleRead":
      handleRead(row);
      break;
    case "handleEdit":
      handleEdit(row);
      break;
    case "handleDelete":
      handleDelete(row);
      break;
  }
}

//新增操作
const handleAdd = () => {
  dialogFormRef.value.show(null, "add");
};

// 查看表格行
function handleRead(row) {
  descriptionsDialogRef.value.show(row, columns1, columns2, columns3);
}

// 修改表格行
function handleEdit(row) {
  dialogFormRef.value.show(row, "edit");
}

// 删除表格行
async function handleDelete(row) {
  ElMessageBox.confirm("确定要删除吗?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  })
    .then(async () => {
      const { code, message } = await deleteProjectBudget({ id: row.id });
      if (code == 0) {
        ElMessage.success(message);
        onSearch();
      }
    })
    .catch(() => {});
}

//导出
function handleExport() {
  const fileName = "项目收付款预算表";
  exportExcel("/api/ProjectBudget/Export?", fileName, queryForm);
}
//导出单条
function handleExportOne(row) {
  const fileName = "项目收付款预算表";
  exportExcel("/api/ProjectBudget/ExportOne?", fileName, { id: row.id });
}

// 表格行选中回调resetForm
function handleSelectionChange(_val) {}

// 设置表格组件高度
const setTableHeight = () => {
  nextTick(() => {
    queryItemMaxNum.value =
      (queryFormRef.value.$el.clientWidth -
        8 -
        32 -
        queryFormRef.value.$el.children[
          queryFormRef.value.$el.children.length - 1
        ].clientWidth) /
      (queryFormRef.value.$el.children[0].clientWidth + 32);
    queryItemMaxNum.value = Math.floor(queryItemMaxNum.value);
    tableHeight.value =
      window.innerHeight - queryFormRef.value.$el.clientHeight - 230;
  });
};

// mounted周期函数
onMounted(async () => {
  getProjectNatureList();
  onSearch();
  setTableHeight();
});

// 窗口尺寸改变事件回调
window.onresize = function () {
  nextTick(() => {
    setTableHeight();
  });
};
</script>

<template>
  <div class="main">
    <el-form
      ref="queryFormRef"
      :inline="true"
      :model="queryForm"
      class="bg-bg_color w-100/100 pl-2 pt-4 mb-2"
      label-width="70px"
    >
      <el-form-item
        v-show="queryItemMaxNum >= 1 || !queryFold"
        label="项目性质"
        prop="projectNature"
      >
        <el-select v-model="queryForm.projectNature" placeholder="">
          <el-option label="全部" value="" />
          <el-option
            v-for="item in projectNatureList"
            :key="item.value"
            :label="item.name"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item
        v-show="queryItemMaxNum >= 2 || !queryFold"
        label="项目名称"
        prop="projectId"
      >
        <el-select
          v-model="queryForm.projectId"
          placeholder="请选择"
          clearable
          filterable
          style="width: 100%"
          @change="handleProjectChange"
        >
          <el-option
            v-for="item in projectList"
            :key="item.projectId"
            :label="item.projectFullName"
            :value="item.projectId"
          />
        </el-select>
      </el-form-item>
      <el-form-item
        v-if="false"
        v-show="queryItemMaxNum >= 3 || !queryFold"
        label="合同编号"
        prop="sellContractId"
      >
        <el-select
          v-model="queryForm.sellContractId"
          placeholder="请选择"
          clearable
          filterable
          :no-data-text="
            queryForm.projectId == '' ? '请先选择项目' : '未获取到数据'
          "
          no-match-text="未匹配到数据"
          style="width: 100%"
        >
          <el-option
            v-for="item in sellContractList"
            :key="item.value"
            :label="item.remark"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          :icon="useRenderIcon('search')"
          :loading="requestLoading"
          @click="onSearch"
        >
          查询
        </el-button>
        <el-button :icon="useRenderIcon('refresh')" @click="handleReset">
          重置
        </el-button>
        <el-button type="text" @click="handleFold">
          <span v-show="queryFold">展开</span>
          <span v-show="!queryFold">折叠</span>
          <IconifyIconOffline :icon="!queryFold ? 'arrow-up' : 'arrow-down'" />
        </el-button>
      </el-form-item>
    </el-form>
    <TableProBar
      :title="title"
      :loading="requestLoading"
      :dataList="tableData"
      @refresh="onSearch"
    >
      <template #buttons>
        <el-button
          v-for="(item, index) in tableButtons"
          :key="index"
          :type="item.buttonType"
          :icon="useRenderIcon(item.buttonIcon)"
          @click="handleOperation(item.buttonClick)"
        >
          {{ item.buttonName }}
        </el-button>
      </template>
      <template v-slot="{ size, checkList }">
        <el-table
          border
          :size="size"
          :height="tableHeight"
          :data="tableData"
          highlight-current-row
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            v-if="checkList.includes('勾选列')"
            type="selection"
            align="center"
            width="60"
          />
          <el-table-column
            v-if="checkList.includes('序号列')"
            fixed="left"
            type="index"
            :index="getIndex"
            label="序号"
            align="center"
            width="60"
          />
          <template v-for="column in columns1" :key="column.prop">
            <el-table-column
              v-if="column.read"
              :label="column.label"
              :prop="column.prop"
              :align="column.align"
              :min-width="column.width"
              :fixed="column.fixed"
              :show-overflow-tooltip="column.showOverflowTooltip"
            >
              <template #default="{ row }">
                <el-text
                  type="primary"
                  class="cursor-pointer"
                  @click="handleOperation('handleRead', row)"
                >
                  {{ row[column.prop] }}
                </el-text>
              </template>
            </el-table-column>
            <el-table-column
              v-else
              :align="column.align"
              :fixed="column.fixed"
              :label="column.label"
              :prop="column.prop"
              :show-overflow-tooltip="column.showOverflowTooltip"
              :min-width="column.width"
              :formatter="column.formatter"
            >
              <template #default="{ row }">
                <template v-if="column.tableColumnSlot">
                  <component :is="column.tableColumnSlot(row)" />
                </template>
                <template v-else-if="column.formatter">
                  {{ column.formatter(row) }}
                </template>
                <template v-else>
                  {{ row[column.prop] }}
                </template>
              </template>
            </el-table-column>
          </template>
          <el-table-column
            label="预算费用明细"
            align="center"
            prop="columnCenter"
            width="110"
          >
            <template v-for="column in columns2" :key="column.prop">
              <el-table-column
                v-if="column.read"
                :label="column.label"
                :prop="column.prop"
                :align="column.align"
                :min-width="column.width"
                :fixed="column.fixed"
                :show-overflow-tooltip="column.showOverflowTooltip"
              >
                <template #default="{ row }">
                  <el-text
                    type="primary"
                    class="cursor-pointer"
                    @click="handleOperation('handleRead', row)"
                  >
                    {{ row[column.prop] }}
                  </el-text>
                </template>
              </el-table-column>
              <el-table-column
                v-else
                :align="column.align"
                :fixed="column.fixed"
                :label="column.label"
                :prop="column.prop"
                :show-overflow-tooltip="column.showOverflowTooltip"
                :min-width="column.width"
                :formatter="column.formatter"
              >
                <template #default="{ row }">
                  <template v-if="column.tableColumnSlot">
                    <component :is="column.tableColumnSlot(row)" />
                  </template>
                  <template v-else-if="column.formatter">
                    {{ column.formatter(row) }}
                  </template>
                  <template v-else>
                    {{ row[column.prop] }}
                  </template>
                </template>
              </el-table-column>
            </template>
          </el-table-column>
          <el-table-column
            label="实际发生费用明细"
            align="center"
            prop="columnCenter"
            width="110"
          >
            <template v-for="column in columns3" :key="column.prop">
              <el-table-column
                v-if="column.read"
                :label="column.label"
                :prop="column.prop"
                :align="column.align"
                :min-width="column.width"
                :fixed="column.fixed"
                :show-overflow-tooltip="column.showOverflowTooltip"
              >
                <template #default="{ row }">
                  <el-text
                    type="primary"
                    class="cursor-pointer"
                    @click="handleOperation('handleRead', row)"
                  >
                    {{ row[column.prop] }}
                  </el-text>
                </template>
              </el-table-column>
              <el-table-column
                v-else
                :align="column.align"
                :label="column.label"
                :prop="column.prop"
                :show-overflow-tooltip="column.showOverflowTooltip"
                :min-width="column.width"
                :fixed="column.fixed"
                :formatter="column.formatter"
              >
                <template #default="{ row }">
                  <template v-if="column.tableColumnSlot">
                    <component :is="column.tableColumnSlot(row)" />
                  </template>
                  <template v-else-if="column.formatter">
                    {{ column.formatter(row) }}
                  </template>
                  <template v-else>
                    {{ row[column.prop] }}
                  </template>
                </template>
              </el-table-column>
            </template>
          </el-table-column>
          <TableColumOperation
            v-if="operationButtons().length > 0"
            :size="size"
            :operationButtons="operationButtons()"
            @operation="handleOperation"
          />
          <template #empty>
            <el-empty description="暂无数据" />
          </template>
        </el-table>
        <el-pagination
          v-model:page-size="queryForm.pageSize"
          v-model:current-page="queryForm.pageIndex"
          class="flex justify-end mt-4"
          :small="size === 'small' ? true : false"
          :page-sizes="[10, 20, 30, 50, 100, 200]"
          :background="true"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalPage"
          @size-change="onSearch"
          @current-change="onSearch"
        />
      </template>
    </TableProBar>
    <DialogForm
      ref="dialogFormRef"
      :projectList="projectList"
      :projectNatureList="projectNatureList"
      @search="onSearch"
    />
    <DescriptionsDialog
      ref="descriptionsDialogRef"
      :title="title"
      :width="1184"
      :column="4"
    />
  </div>
</template>
